package com.example.Lezedata.ui.fragment.commonfragment;

import androidx.annotation.RequiresApi;
import androidx.lifecycle.ViewModelProvider;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.ui.activity.setting.ForgetPINVc;
import com.example.Lezedata.ui.fragment.BaseDiaLogFragment;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;

import static android.view.KeyEvent.ACTION_UP;
import static android.view.KeyEvent.KEYCODE_DEL;

public class TransactionPinView extends BaseDiaLogFragment {

    public interface InputListener{
        void InputComplete(String text) throws NoSuchAlgorithmException;
    }

    public InputListener inputListener;
    private TransactionPinViewViewModel mViewModel;

    private TextView editText1;
    private TextView editText2;
    private TextView editText3;
    private TextView editText4;
    private EditText inputEdit;
    private TextView Forget;

    ArrayList<TextView> editTexts = new ArrayList<TextView>();

    public static TransactionPinView newInstance() {
        return new TransactionPinView();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.transaction_pin_view_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(TransactionPinViewViewModel.class);
        // TODO: Use the ViewModel
        inputEdit.requestFocus();
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    public void initView() {
        super.initView();
        editText1 = findViewById(R.id.editText1);
        editText2 = findViewById(R.id.editText2);
        editText3 = findViewById(R.id.editText3);
        editText4 = findViewById(R.id.editText4);
        inputEdit = findViewById(R.id.inputEdit);
        Forget = findViewWithTag("Forget");

        editTexts.add(editText1);
        editTexts.add(editText2);
        editTexts.add(editText3);
        editTexts.add(editText4);

        editText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputEdit.requestFocus();
            }
        });

        findViewById(R.id.pin_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TransactionPinView.super.dismiss();
            }
        });

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.d(TAG, "beforeTextChanged: ");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d(TAG, "onTextChanged: ");
            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    refreshEDT();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "afterTextChanged: ");
            }
        };

        Forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                baseVc().jumpActivity(ForgetPINVc.class);
            }
        });

        inputEdit.addTextChangedListener(watcher);
        inputEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        });
    }

    void refreshEDT() throws NoSuchAlgorithmException {
        String string = inputEdit.getText().toString();
        for (int i = 0 ; i < editTexts.size() ; i++) {
            TextView editText = editTexts.get(i);
            String str = null;
            if (string.length() > i){
                str = string.substring(i,i+1);
            }
            editText.setText(str);
        }
        if (string.length() >= 4){
            if (inputListener != null){
                inputListener.InputComplete(string);
            }
        }
    }


    @Override
    public int windowAnimationsType() {
        return R.style.popWindow_anim_style;
    }
}